<?php

namespace app\web\controller;
use app\web\model\CashModel;
use app\web\model\LineMemberModel;
use app\web\model\LineMemberLevelModel;
use app\web\model\LineAttributesModel;
use app\web\model\LineRechlogModel;
use app\web\model\LineExchangeLogModel;
use app\web\model\LineExchangeItemModel;
use app\web\model\LineMemchargeModel;
use app\web\model\LineMemchargeLogModel;
use app\common\model\Member;
use app\common\controller\StoreBaseController;
use think\Db;
use app\common\model\Prt;
/**
 * 收银
 */
class CashierController extends StoreBaseController{
	protected $model;
 	protected $attr_model;
	protected $rechlog_model;
	protected $memchargelog_model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new LineMemberModel();
		//自定义属性
		$this->attr_model = new LineAttributesModel();
		//充值记录
		$this->rechlog_model = new LineRechlogModel();
		//充次记录
		$this->memcharge_model = new LineMemchargeModel();
		$this->memchargelog_model = new LineMemchargeLogModel();
		//兑换记录
		$this->exchangelog_model = new LineExchangeLogModel();
		$this->exchangeitem_model = new LineExchangeItemModel();
		//会员等级
		$this->modelevel = new LineMemberLevelModel();
        $member_level = $this->modelevel->select();
		$this->assign('member_level',$member_level);
        
    }
	/**
	 * 打印订单
	 */
	public function print_order(){
		$prt=new Prt();	
		$prt->print_order2(input("sn"));
	}
	/**
	 * 查询我的店铺优惠券
	 */
	public function select_juan(){
		$store= session('storeInfo');
		$where['a.store_id']=$store['id'];
		$where['a.member_id']=input("member_id");
		$where['a.status']=1;
		$coups=db('line_couponrecord')->alias("a")->join("line_coupon c","a.coupon_id=c.id")->where($where)->field("a.*,c.name,c.typeid,c.parvalue,c.discount")->select();
		$this->assign("list",$coups);
		return $this->fetch();
	}
	/**
	 * 增加房台订单
	 */
	public function add_house(){
		$house_id=input("id");
		$status=input("status");
		$where['house_id']=$house_id;
		$where['status']=1;
		$dt=input('post.');
		$gds=$dt['data'];
		$ot=1;//是否属于加单
		$items=array();
		$add_time=now_time();
		if($status==1){//已经添加过商品
			$ot=2;//属于加单
			Db::name('line_order')->where($where)->setInc('price', input("price"));
			$order=db("line_order")->where($where)->find();
			$orderId=$order['id'];
		}else{
			$sn=order_create_no();
			$param['sn']=$sn;
			$param['pay_time']=now_time();
			$param['add_time']=now_time();
			$param['price']=input("price");
			$param['house_id']=input("id");
			$param['type']=input("type");
			$store= session('storeInfo');
			if($store['id']){
				$param['agent_id']=$store['id'];
			}else{
				return json(['code'=>405,'msg'=>"错误 "]);
			}
			$res=Db::name('line_order')->insert($param);
			$orderId = Db::name('line_order')->getLastInsID();
		}
		foreach($gds as $i){
			  $items[]=array("ot"=>$ot,"add_time"=>$add_time,"order_id"=>$orderId,"type"=>$i['type'],"sid"=>$i['sid'],"price"=>$i['price'],"name"=>$i['name'],"num"=>$i['num'],"goods_names"=>$i['goods_names']);
		}
		if($orderId){
			$res1=Db::name('line_orderitems')->insertAll($items);
			if($res1){
				 $list=db("line_orderitems")->where("order_id",$orderId)->select();
				 return json(['code'=>200,'data'=>$list,'orderId'=>$orderId]);
			}else{
				 return json(['code'=>406,'msg'=>"错误 "]);
			}
		}
		return json(['code'=>407,'msg'=>"错误 "]);
		
	}
	/**
	 * 更新子订单详情
	 */
	public function update_item(){
		$item_id=input("item_id");
		$order_id=input('order_id');
		$update['update_time']=now_time();
		if(input("num")){//修改数量
			$update['num']=input("num");
		}
		if(input("is_zs")){
			$update['is_zs']=input("is_zs");//是否赠送
		}
		if(input("is_del")){//删除子订单
			$res=db("line_orderitems")->delete($item_id);
		}else{
			$res=db("line_orderitems")->where("id",$item_id)->update($update);
		}
		if($res){
			$set=" update_time = '".now_time()."'";
			if(input("change_price")){
				$set.=",price=price+".input("change_price");
			}
			$sql="update long_line_order set ".$set." where id=$order_id";
			$res2=Db::execute($sql);
			return json(['code'=>200]);
		}
		return json(['code'=>406]);
	}
	/**
	 * 获取线下订单
	 */
	public function get_orders(){
		$data=input("");
		if($data['status']){
			$where['status']=$data['status'];
		}
		$store= session('storeInfo');
		$where['agent_id']=$store['id'];
		$list=db("line_order")->where($where)->select();
		if($list){
			foreach($list as $k=>$v){
				$list1[$v['id']]=$v;
			}
			$order_ids = array_keys($list1);
			$order_items = db("line_orderitems")->where("order_id","in",$order_ids)->select();
			foreach($order_items as $k1=>$v1){
				 $list1[$v1['order_id']]['items'][]=$v1;
			}
			return json(["list"=>$list1]);
		}
		return json(["list"=>null]);		
	}
	/**
	 * 根据订单id获取订单
	 */
	public function jiegua(){
		$order=db("line_order")->find(input("id"));
		$list=db("line_orderitems")->where("order_id",input("id"))->select();
		db("line_order")->delete(input("id"));
		db("line_orderitems")->where("order_id",input("id"))->delete();
		return json(["list"=>$list,"order"=>$order]);
	}
	/**
	 * 删除挂单的订单
	 */
	public function del_order(){
		db("line_order")->delete(input("id"));
		db("line_orderitems")->where("order_id",input("id"))->delete();
		echo 1;
		exit;
	}
	/**
	 * 挂单
	 */
	public function guadan(){
		$data=input('post.');
		$gds=$data['data'];
		$sn=order_create_no();
		$param['sn']=$sn;
		$param['pay_time']=now_time();
		$param['add_time']=now_time();
		$param['price']=$data['price'];
		$param['memo']=$data['memo'];
		$store= session('storeInfo');
		if($store['id']){
			$param['agent_id']=$store['id'];
		}else{
			return json(['code'=>408,'msg'=>"挂单失败 "]);
		}
		$employee=session('employee');
		if($employee){
			$param['emp_id']=$employee['id'];
		}
		// 启动事务
		Db::startTrans();
		try{
			$res=Db::name('line_order')->insert($param);
			$orderId = Db::name('line_order')->getLastInsID();
			$items=array();
			$add_time=now_time();
			$price=0;
			foreach($gds as $i){
			  $items[]=array("is_zs"=>$i['is_zs'],"add_time"=>$add_time,"order_id"=>$orderId,"type"=>$i['type'],"sid"=>$i['sid'],"price"=>$i['price'],"name"=>$i['name'],"num"=>$i['num'],"goods_names"=>$i['goods_names']);
			}
			
			$res1=Db::name('line_orderitems')->insertAll($items);
		    // 提交事务
			if($res1){
				Db::commit();    
				echo 1;
				exit;
			}
			Db::rollback();
			echo 2;
			exit;
		} catch (\Exception $e) {
			var_dump($e);
		    // 回滚事务
		    Db::rollback();
			echo 3;
			exit;
		}
		exit;
	}
	/**
	 * 判断是否付款成功
	 */
	public function confirm_pay(){
		if(input("type")){
			if(input("type")==6){
				$order=db('line_rechlog')->where("sn",input("sn"))->find();	
			}else if(input("type")==7){
				$order=db('line_memcharge')->where("sn",input("sn"))->find();	
			}
		}else{
			$order=db('line_order')->where("sn",input("sn"))->find();			
		}
		if($order&&$order['status']==2){
			echo 1;
		}else{
			echo 2;
		}
		exit;
		
	}
	/**
	 * 收款
	 */
	public function shoukuan(){
		 if(request()->isAjax()){
        	$param=input('post.');
			 if(isset($param['data'])){
			 	$gds=$param['data'];//购物车商品列表
				 unset($param['data']);
			 }
			$pay_method=$param['pay_method'];
			$sn=order_create_no();
			$param['sn']=$sn;
			$param['pay_time']=now_time();
			$param['add_time']=now_time();
			$store= session('storeInfo');
			$zs_score=$param['zs_score'];
			if($param['line_member_id']&&$store['score']<$zs_score){
				return json(['code'=>408,'msg'=>"收款失败！当前店铺积分 ".$store['score'].',未达到赠送积分数目']);
			}
			$data['sn']=$sn;
			$data['add_time']=now_time();
			$data['store_name']=$store['store_name'];
			if($store['id']){
				$param['agent_id']=$store['id'];
			}else{
				return json(['code'=>408,'msg'=>"收款失败 "]);
			}
			$employee=session('employee');
			if($employee){
				$param['emp_id']=$employee['id'];
				$data['skr']=$employee['no'];
			}else{
				$data['skr']=$store['store_tel'];
			}
			if($pay_method==1||$pay_method==2||$pay_method==5){
					$param['status']=2;//现金会员卡收费订单直接完成微信支付吗需要在回调后处理
			}
			if($param['type']==1||$param['type']==2){//快速消费商品消费
				$res=db('line_order')->insert($param);
				$orderId = db('line_order')->getLastInsID();
				if($param['type']==2){//商品消费增加订单详情
					foreach($gds as $i){
					  $items[]=array("is_zs"=>$i['is_zs'],"add_time"=>now_time(),"order_id"=>$orderId,"type"=>$i['type'],"sid"=>$i['sid'],"price"=>$i['price'],"name"=>$i['name'],"num"=>$i['num'],"goods_names"=>$i['goods_names']);
					}
					$res1=db('line_orderitems')->insertAll($items);
				}
			}elseif($param['type']==4){//房台消费
					$orderId=$param['orderId'];
					unset($param['orderId']);
					$house_id=input("house_id");
					$res=db("line_order")->where('id',$orderId)->update($param);
					if($pay_method==1||$pay_method==2||$pay_method==5){
						 db("line_table")->where("id",$house_id)->update(['status'=>1]);	
					}
			}
			if($pay_method==1||$pay_method==2||$pay_method==5){
				if($param['line_member_id']){
					//赠送积分
					$set=" newbonusflg=newbonusflg+"."$zs_score, total_score=total_score+$zs_score";
					if($pay_method==1||$pay_method==5){//银行卡现金消费修改累计消费金额积分
					 	//Db::name('line_member')->where('id', $param['line_member_id'])->setDec('newblanceflg', $param['price']);
						$set.=", total_cost=total_cost+".input("price");
					}else if($pay_method==2){//会员卡扣除金额
						$set.=", newblanceflg = newblanceflg-'".$param['price']."'";
					}
					$sql="update long_line_member set ".$set." where id=".$param['line_member_id'];
					Db::execute($sql);
					if($param['coupon_id']){
					db("line_couponrecord")->where("id",$param['coupon_id'])->update(["status"=>2]);
					}
					$member_model=new Member();
					$result=$member_model -> insscoch($store['id'], '消费扣除积分', -$zs_score, 4, $orderId);//扣除店铺的积分
					
					if($result){
						$store['score']=$store['score']-$zs_score;
						session('storeInfo',$store);
					}
					$line_member=db("line_member")->find($param['line_member_id']);
					$member=db("member")->where("mobile",$line_member['mobile'])->find();
					if($member){
						$member_model->mc_credit_inc($member, 'credit1', $zs_score, 0, '消费获得积分',4,$orderId);//增加线上会员积分						
					}
					$line=new LineMemberModel();//增加会员消费记录
					$line->add_line_paylog($param);
				}
			}elseif($pay_method==3){//微信支付
				$payment = new \app\api\controller\PaymentController();
				$code_url=$payment->getcode($param);
				$data['code_url']=$code_url;
			}elseif($pay_method==4){//支付宝支付
				$payment = new \app\api\controller\PaymentController();
				$code_url=$payment->alicode($param);
				$data['code_url']=$code_url;
			}
			return json(['code'=>200,'data'=>$data]);
			
		 }else{
		 	 return json(['code'=>408,'msg'=>"收款失败 "]);
		 }
	}
	/**
	 * 添加线下会员
	 */
	public function add_member(){
		if(request()->isAjax()){
        	$param=input('post.');
			$param['reg_date'] = now_time();
			$param['store_id'] = session('storeInfo')['id'];
			$param['store_name'] = session('storeInfo')['store_name'];
			
			Db::name('line_member')->insert($param);
			$mId = Db::name('line_member')->getLastInsID();
			if ($mId) {
				$member_model=new Member();
				$res=$member_model->add($param['mobile'], session('storeInfo')['u_id']);
               return json(['code'=>200,'msg'=>"保存成功",'data'=>Db::name('line_member')->find($mId)]);
            } else {
               return json(['code'=>408,'msg'=>"保存失败"]);
            }
        }else{
        	//会员等级
	        $member_level =db("line_memberlevel")->select();
			$this->assign('member_level',$member_level);
			$this->assign('attr',$this->attr_model->where('store_id',session('storeInfo')['id'])->find());
			return $this->fetch();
        }
		
	}
	/**
	 * 搜索会员
	 */
	public function get_emps(){
		$key=input("key");
		$store_id=session('storeInfo')['id'];
		if($key){
		 $like="%$key%";
		 $list = db("line_member")->where("store_id",$store_id)->where('member_no|membername|mobile', 'like', $like)->select();
		}else{
		 $list = db("line_member")->where("store_id",$store_id)->limit(10)->select();
		}
		exit(json_encode($list));
	}
	/**
	 * 根据工号查找员工
	 */
	public function get_emp(){
		$emp=db("line_employee")->alias('a')->join('line_zhiwei b ','b.id= a.z_id')->field("a.*,b.name as zname")->where("no",input("no"))->find();
		exit(json_encode($emp));
	}
	/**
	 * 快速消费
	 */	
	public function fast(){
		return $this->fetch();
	}
	/**
	 * 商品消费
	 */
	public function goods(){
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		$goods =db("line_goods")->where($where)->select();
		$packges=db("line_package")->where($where)->select();
		$this->assign("packges",$packges);
		$this->assign("goods",$goods);
		$types=$this->get_types($store_id);
		$this->assign("types",$types);
		return $this->fetch();
	}
	/**
	 * 搜索商品列表
	 */
	public function get_goods_list(){
		$param=input('post.');
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		if(isset($param['goods_typeid'])&&$param['goods_typeid']){
			$where['goods_typeid']=$param['goods_typeid'];
		}
		if(isset($param['key'])&&$param['key']){
			$like="%".$param['key']."%";
			$data=db("line_goods")->where($where)->where('goods_no|goods_name',"like",$like)->select();
		}else{
			$data=db("line_goods")->where($where)->select();
		}
		return json(['code'=>200,'data'=>$data]);
		exit;
	}
	
	/**
	 * 搜索套餐列表
	 */
	public function get_package_list(){
		$param = input('post.');
		$store_id = session('storeInfo')['id'];
		$where["store_id"] = $store_id;
		$where['package_type'] = '2';		
		if(isset($param['key'])&&$param['key']){
			$like="%".$param['key']."%";		
			$data=db("line_package")->where($where)->where('package_no|package_name',"like",$like)->select();
		}else{
			$data=db("line_package")->where($where)->select();
		}
		return json(['code'=>200,'data'=>$data]);
		exit;
	}
	
	/**
	 * 搜索商品列表
	 */
	public function get_goods_lists(){
		$param=input('post.');
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		if(isset($param['goods_typeid'])&&$param['goods_typeid']){
			$where['goods_typeid']=$param['goods_typeid'];
		}
		if(isset($param['key'])&&$param['key']){
			$like=$param['key'];
			$data=db("line_goods")->where($where)->where('goods_no',$like)->select();
		}
		return json(['code'=>200,'data'=>$data]);
		exit;
	}
	
	/**
	 * 搜索套餐列表
	 */
	public function get_package_lists(){
		$param = input('post.');
		$store_id = session('storeInfo')['id'];
		$where["store_id"] = $store_id;	
		if(isset($param['key'])&&$param['key']){
			$like=$param['key'];		
			$data=db("line_package")->where($where)->where('package_no',$like)->select();
		}
		return json(['code'=>200,'data'=>$data]);
		exit;
	}
	
	/**
	 * 获取房台分类
	 */
	public function get_type($store_id){
		$cache=cache("housetype");
		$where["store_id"]=$store_id;
		if($cache){
			if(isset($cache[$store_id])){
				$types=$cache[$store_id];
			}
			$types=db("line_tabletype")->where($where)->select();
			$cache[$store_id]=$types;
			cache("housetype",$cache);
		}else{
			$types=db("line_tabletype")->where($where)->select();
			$cache[$store_id]=$types;
			cache("housetype",$cache);
		}
		return $types;
	}
	
	/**
	 * 获取商品分类
	 */
	public function get_types($store_id){
		$cache=cache("goodstypes");
		$where["agent_id"]=$store_id;
		if($cache){
			if(isset($cache[$store_id])){
				$types=$cache[$store_id];
			}else{
				$types=db("mall_store_goodstype")->where($where)->select();
				$cache[$store_id]=$types;
				cache("goodstypes",$cache);
			}
		}else{
			$types=db("mall_store_goodstype")->where($where)->select();
			$cache[$store_id]=$types;
			cache("goodstypes",$cache);
		}
		return $types;
	}
	
	/**
	 * 扫码消费
	 */
	public function scan(){
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		$this->assign("packges",'');
		$this->assign("goods",'');
		$types=$this->get_types($store_id);
		//获取操作员信息
		$employee=session('employee');
		if($employee){
			$mobile = $employee['mobile'];
		}else{
			$mobile = session('storeInfo')['store_tel'];				
		}
		$this->assign("types",$types);
		$this->assign('mobile',$mobile);
		$this->assign('store_name',session('storeInfo')['store_name']);
		return $this->fetch();
	}
	
	/**
	 * 开关启房台
	 */
	public function update_house(){
		$house_id=input('id');
		$res=db("line_table")->where("id",$house_id)->update(["status"=>input("status"),"add_time"=>now_time()]);
		if($res){
			if(input("status")==1){//关台
				$house=db("line_table")->find($house_id);
				$where['house_id']=$house_id;
				$where['status']=1;
			    db("line_order")->where($where)->update(["status"=>3]);
			}
			return json(['code'=>200,'msg'=>'操作成功']);
		}
		return json(['code'=>408,'msg'=>'操作失败']);
	}
	/**
	 * 查询多房台
	 */
	public function get_houses(){
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		if(input("status")){
			$where['status']=input("status");
		}
		if(input("typeid")){
			$where['table_typeid']=input("typeid");
		}
		$list=db("line_table")->where($where)->select();
		if($list){
			return json(['code'=>200,'data'=>$list]);
		}else{
			return json(['code'=>404,'msg'=>'该分类下没有房台!']);
		}
	}
	/**
	 * 获取房台信息
	 */
	public function get_house(){
		$house=db("line_table")->find(input("id"));
		if($house['status']==1){
			return json(['code'=>404,'msg'=>'未开台']);
		}
		$where['house_id']=input("id");
		$where['status']=1;
		$order=db("line_order")->where($where)->find();
		if($order){
			$list=db("line_orderitems")->where("order_id",$order['id'])->select();
			$order['item']=$list;
			return json(['code'=>200,'data'=>$order,'kt_time'=>$house['add_time']]);
		}
		return json(['code'=>406,'msg'=>'未添加商品','kt_time'=>$house['add_time']]);
	}
	/**
	 *  转台
	 */
	 public function zhuantai(){
	 	$old_id=input("old_id");
		$new_id=input("new_id");
		$status=input("status");
		$where['id']=$old_id;
		$where['status']=2;
		$where_new['id']=$new_id;
		$where_new['status']=1;
		$res=db("line_table")->where($where)->update(["status"=>1]);//关闭旧台
		if($res){
			$res1=db("line_table")->where($where_new)->update(["status"=>2]);//开启新台
			if($status){//如果旧台已经添加商品则修改旧台订单为新台订单
				$where2["house_id"]=$old_id;
				$where2['status']=1;
				$res2=db("line_order")->where($where2)->update(["house_id"=>$new_id]);//开启新台
				if($res2){
					return json(['code'=>200,'msg'=>'转台成功']);
				}else{
					return json(['code'=>406,'msg'=>'错误3']);
				}
			}
			return json(['code'=>406,'msg'=>'错误2']);
		}
		return json(['code'=>406,'msg'=>'错误1']);
	 }
	/**
	 * 房台消费
	 */
	public function house(){
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		$houses =db("line_table")->where($where)->select();
		$goods =db("line_goods")->where($where)->select();
		$packges=db("line_package")->where($where)->select();
		$this->assign("packges",$packges);
		$this->assign("goods",$goods);
		$this->assign("houses",$houses);
		$types=$this->get_types($store_id);
		$this->assign("types",$types);
		$tabletypes=$this->get_type($store_id);
		$this->assign("tabletypes",$tabletypes);
		return $this->fetch();
	}
	/**
	 * 获取会员商品计次充值记录
	 */
	public function getjici(){
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		if(input("member_id")){
			$where['line_member_id']=input("member_id");
		}
		$list=db("line_memcharge")->where($where)->select();
		if($list){
			return json(['code'=>200,'data'=>$list]);
		}else{
			return json(['code'=>404,'msg'=>'没有计次充值']);
		}
	}
	/**
	 * 会员计次消费
	 */
	public function jici(){
		$store_id=session('storeInfo')['id'];
		$where["store_id"]=$store_id;
		$where["store_id"]=$store_id;
		$goods = db("line_goods")->where('store_id',$store_id)->select();
		$packges = db("line_package")->where('store_id',$store_id)->where('package_type','2')->select();
		$this->assign("packges",$packges);
		$this->assign("goods",$goods);
		$types=$this->get_types($store_id);
		$this->assign("types",$types);
		return $this->fetch();
	}
	/**
	 * 扣次
	 */
	public function kouci(){
		$params=input("");
		$gds=$params['data'];
		$member_id=input("line_member_id");
		$memo="";
		foreach($gds as $i){
			 $num=$i['num'];
			 $id=$i['id'];
			 $memo.=$i['name']."--扣除--".$i['num']."次---";
			 $sql="update long_line_memcharge set num=num-$num, xiaofei_num=xiaofei_num+$num where id=$id";
			 Db::execute($sql);
		}
		$log_no=order_create_no();
		$param['log_no']=$log_no;
		$param['add_time']=now_time();
		$param['start_time']=now_time();
		$param['end_time']=now_time();
		$param['type']=2;
		$param['memo']=$memo;
		$param['line_member_id']=$member_id;
		$store= session('storeInfo');			
		if($store['id']){
			$param['store_id']=$store['id'];
			$param['store_name']=$store['store_name'];							
		}else{
			return $this->error('扣次失败！');	
		}
		$employee=session('employee');
		if($employee){
			$param['mobile'] = $employee['mobile'];//员工手机
		}else{
			$param['mobile'] = $store['store_tel'];//管理员手机
		}
		$res=$this->memchargelog_model->allowField(true)->save($param);
		$data['log_no']=$log_no;
		if($employee){
				$data['skr']=$employee['mobile'];
		}else{
				$data['skr']=$store['store_tel'];
		}
		$data['time']=now_time();
		return json(['code'=>200,'data'=>$data]);
	}
	/**
	 * 会员充值
	 */
	public function recharge(){
		$employee=session('employee');
			if($employee){
				$mobile = $employee['mobile'];
			}else{
				$mobile = session('storeInfo')['store_tel'];				
			}
		$this->assign('mobile',$mobile);
		$this->assign('store_name',session('storeInfo')['store_name']);
		return $this->fetch();
	}
	/**
	 * 会员冲次
	 */
	public function chongci(){
		$store_id = session('storeInfo')['id'];
		$goods = db("line_goods")->where('store_id',$store_id)->select();
		$packges = db("line_package")->where('store_id',$store_id)->where('package_type','2')->select();
		$this->assign("packges",$packges);
		$this->assign("goods",$goods);
		$types=$this->get_types($store_id);
		//获取操作员信息
		$employee=session('employee');
		if($employee){
			$mobile = $employee['mobile'];
		}else{
			$mobile = session('storeInfo')['store_tel'];				
		}
		$this->assign('mobile',$mobile);
		$this->assign('store_name',session('storeInfo')['store_name']);
		$this->assign("types",$types);
		return $this->fetch();
	}
	
	/**
	 * 充次收款
	 */
	public function chongcishouk(){
		if(request()->isAjax()){
        	$param=input('post.');
			if($param['data']){
			 	$gds=$param['data'];//购物车商品列表
				unset($param['data']);
			 }
			$log_no=order_create_no();
			$param['sn']=$log_no;
			$param['add_time']=now_time();
			$param['start_time']=now_time();
			$param['end_time']=date("Y-m-d 23:59:59",strtotime("+1 year"));
			$store= session('storeInfo');			
			if($store['id']){
				$param['store_id']=$store['id'];
				$param['agent_id']=$store['id'];
				$param['store_name']=$store['store_name'];							
			}else{
				return $this->error('充次失败！');	
			}
			$employee=session('employee');
			if($employee){
				$param['mobile'] = $employee['mobile'];//员工手机
			}else{
				$param['mobile'] = $store['store_tel'];//管理员手机
			}
			$pay_method=$param['pay_method'];
			if($pay_method==1||$pay_method==5||$pay_method==2){//现金会员卡银联直接完成
				$param['status']=2;
				if($pay_method==1||$pay_method==5){//银行卡现金消费修改累计消费金额
					 	//Db::name('line_member')->where('id', $param['line_member_id'])->setDec('newblanceflg', $param['price']);
						$set=" total_cost=total_cost+".input("price");
				}else if($pay_method==2){//会员卡扣除金额
					$set=" newblanceflg = newblanceflg-'".$param['price']."'";
				}
				$sql="update long_line_member set ".$set." where id=".$param['line_member_id'];
				Db::execute($sql);
			}
			if($this->memchargelog_model->allowField(true)->save($param) !== false){
				//添加充次关系记录
				$items2 = array();
				$logId = Db::name('line_memchargelog')->getLastInsID();
				foreach($gds as $i){
		  			$items[]=array("log_id"=>$logId,"type"=>$i['type'],"sid"=>$i['sid'],"price"=>$i['price'],"name"=>$i['name'],"num"=>$i['num'],"goods_names"=>$i['goods_names']);
					//操作充次记录表
					$memcharge = $this->memcharge_model->where('sid', $i['sid'])->where('line_member_id', $param['line_member_id'])->where('store_id', $store['id'])->find();						
					if($memcharge){
						Db::name('line_memcharge')->where('id', $memcharge['id'])->setInc('num', $i['num']);
					}else{
						$items2[]=array("sn"=>$log_no,"update_time"=>now_time(),"type"=>$i['type'],"sid"=>$i['sid'],"name"=>$i['name'],"num"=>$i['num'],"store_id"=>$store['id'],"line_member_id"=>$param['line_member_id']);													
					}
				}	
				$res1=Db::name('line_memchargeitem')->insertAll($items);
				if($pay_method==1||$pay_method==5||$pay_method==2){//现金会员卡银联直接完成生成记录
					$res2=Db::name('line_memcharge')->insertAll($items2);	
				}elseif($pay_method==3){//微信支付
					$payment = new \app\api\controller\PaymentController();
					$code_url=$payment->getcode($param);
					$data['code_url']=$code_url;
				}elseif($pay_method==4){//支付宝支付
					$payment = new \app\api\controller\PaymentController();
					$code_url=$payment->alicode($param);
					$data['code_url']=$code_url;
				}
				//返回前台数据
				$data['member_no'] = $param['no'];
				$data['log_no'] = $log_no;
				$data['add_time'] = now_time();
				$data['store_name'] = $store['store_name'];
				$line_member = $this->model->where('id', $param['line_member_id'])->find();
				$data['newbonusflg'] = $line_member['newbonusflg'];
				$data['newblanceflg'] = $line_member['newblanceflg'];
				$data['mobile'] = $line_member['mobile'];
				if($employee){
					$data['skr']=$employee['mobile'];
				}else{
					$data['skr']=$store['store_tel'];
				}
				return json(['code'=>200,'data'=>$data]);
			}else{
				return $this->error('充次失败！');
			}
			
		 }else{
		 	return $this->error('充次失败！');
		 }
	}
	
	/**
	 * 充值
	 */
	public function chongzhi(){
		if(request()->isAjax()){
        	$param=input('post.');
			$log_no=order_create_no();
			$param['sn']=$log_no;
			$param['add_time']=now_time();
			$store= session('storeInfo');			
			if($store['id']){
				$param['store_id']=$store['id'];
				$param['agent_id']=$store['id'];
				$param['store_name']=$store['store_name'];							
			}else{
				return $this->error('充值失败');	
			}
			$employee=session('employee');
			if($employee){
				$param['mobile'] = $employee['mobile'];//员工手机
			}else{
				$param['mobile'] = $store['store_tel'];//管理员手机
			}
			$pay_method=$param['pay_method'];
			if($pay_method==1||$pay_method==5){//现金银行卡直接完成
				//累积金额、累积积分、累积充值积分、累积充值金额、累积充值次数
				$param['status']=2;
				$price = $param['price'];
				$zs_score = $param['zs_score'];
				$id = $param['line_member_id'];
				$sql="update long_line_member set total_cost=total_cost+".$param['price']."newblanceflg=newblanceflg+$price, newbonusflg=newbonusflg+".
				"$zs_score, total_score=total_score+$zs_score, total_recharge=total_recharge+$price, total_recount=total_recount+1 where id=$id";
			 	Db::execute($sql);
			}
			if($this->rechlog_model->allowField(true)->save($param) !== false){
				$data['member_no'] = $param['no'];
				$data['log_no'] = $log_no;
				$data['add_time'] = now_time();
				$data['store_name'] = $store['store_name'];
				$line_member = $this->model->where('id', $param['line_member_id'])->find();
				$data['newbonusflg'] = $line_member['newbonusflg'];
				$data['newblanceflg'] = $line_member['newblanceflg'];
				$data['mobile'] = $line_member['mobile'];
				if($employee){
					$data['skr']=$employee['mobile'];
				}else{
					$data['skr']=$store['store_tel'];
				}
				if($pay_method==3){//微信支付
					$payment = new \app\api\controller\PaymentController();
					$code_url=$payment->getcode($param);
					$data['code_url']=$code_url;
				}elseif($pay_method==4){//支付宝支付
					$payment = new \app\api\controller\PaymentController();
					$code_url=$payment->alicode($param);
					$data['code_url']=$code_url;
				}
				return json(['code'=>200,'data'=>$data]);
			}else{
				return $this->error('充值失败');
			}
			
		 }else{
		 	return $this->error('充值失败');
		 }
	}
	
	/**
	 * 积分兑换
	 */
	public function exchange(){
		$store_id = session('storeInfo')['id'];
		$goods = db("line_goods")->where('store_id',$store_id)->where('bouns_flag',1)->select();
		$this->assign("goods",$goods);
		$types=$this->get_types($store_id);
		//获取操作员信息
		$employee=session('employee');
		if($employee){
			$mobile = $employee['mobile'];
		}else{
			$mobile = session('storeInfo')['store_tel'];				
		}
		$this->assign('mobile',$mobile);
		$this->assign('store_name',session('storeInfo')['store_name']);
		$this->assign("types",$types);
		return $this->fetch();
	}
	
	/**
	 * 积分兑换
	 */
	public function exchangecz(){
		if(request()->isAjax()){
        	$param=input('post.');
			if($param['data']){
			 	$gds=$param['data'];//购物车商品列表
				unset($param['data']);
		 	}
			$log_no=order_create_no();
			$param['log_no']=$log_no;
			$param['add_time']=now_time();
			$store= session('storeInfo');			
			if($store['id']){
				$param['store_id']=$store['id'];
				$param['store_name']=$store['store_name'];							
			}else{
				return $this->error('兑换失败');	
			}
			$employee=session('employee');
			if($employee){
				$param['mobile'] = $employee['mobile'];//员工手机
			}else{
				$param['mobile'] = $store['store_tel'];//管理员手机
			}
			if($param['line_member_id']){
				//积分
				$pay_score = $param['pay_score'];
				$id = $param['line_member_id'];
				Db::name('line_member')->where('id', $param['line_member_id'])->setDec('newbonusflg', $pay_score);
			}
			
			if($this->exchangelog_model->allowField(true)->save($param) !== false){
				//添加兑换子表记录
				$logId = Db::name('line_exchangelog')->getLastInsID();
				//兑换日志子表
				foreach($gds as $i){
		  			$items[]=array("log_id"=>$logId,"is_zs"=>$i['is_zs'],"sid"=>$i['sid'],"score"=>$i['exchange_bouns'],"name"=>$i['name'],"num"=>$i['num'],"goods_names"=>$i['goods_names']);					
				}	
				$res1=Db::name('line_exchangeitem')->insertAll($items);

				$data['member_no'] = $param['no'];
				$data['log_no'] = $log_no;
				$data['add_time'] = now_time();
				$data['store_name'] = $store['store_name'];
				$line_member = $this->model->where('id', $param['line_member_id'])->find();
				$data['newbonusflg'] = $line_member['newbonusflg'];
				$data['newblanceflg'] = $line_member['newblanceflg'];
				$data['mobile'] = $line_member['mobile'];
				if($employee){
					$data['skr']=$employee['mobile'];
				}else{
					$data['skr']=$store['store_tel'];
				}
				return json(['code'=>200,'data'=>$data]);
			}else{
				return $this->error('兑换失败');
			}
			
		 }else{
		 	return $this->error('兑换失败');
		 }
	}
	
	/**
	 * 退货回款
	 */
	public function reback(){
		$map['a.agent_id']= session('storeInfo')['id'];
        //搜索一栏
		if(request()->isPost()){
		 	$param = input('post.');
			//订单号
			if($param['sn']){
				$map['a.sn'] = ['like', '%'.$param['sn'].'%'];
			}
			//消费类型
			if($param['pay_method'] != 0){
				$map['a.pay_method'] = $param['pay_method'];
			}
			//备注
			if($param['memo']){
				$map['a.memo'] = ['like', '%'.$param['memo'].'%'];
			}
			//会员卡号
			if($param['member_no']){
				$map['member_no'] = ['like', '%'.$param['member_no'].'%'];
			}		
		}
		$field = 'a.*,b.id,b.member_no';		
		$list=db("line_order")->alias('a')->join('line_member b','a.line_member_id = b.id','LEFT')->field($field)->where($map)->paginate(10);
        return $this->fetch('reback', ['list' => $list]); 
	}
	/**
	 * 未完成订单
	 */
	public function unorder(){
		$map['a.agent_id']= session('storeInfo')['id'];
	    $map['a.status']= 1;	
        //搜索一栏
		if(request()->isPost()){
		 	$param = input('post.');
			//订单号
			if($param['sn']){
				$map['a.sn'] = ['like', '%'.$param['sn'].'%'];
			}
			//消费类型
			if($param['pay_method'] != 0){
				$map['a.pay_method'] = $param['pay_method'];
			}
			//收退款金额
			if($param['pay_money']){
				$map['a.pay_money'] = ['like', '%'.$param['pay_money'].'%'];
			}
			//会员卡号
			if($param['member_no']){
				$map['member_no'] = ['like', '%'.$param['member_no'].'%'];
			}		
		}
		$field = 'a.*,b.id,b.member_no';		
		$list=db("line_order")->alias('a')->join('line_member b','a.line_member_id = b.id','LEFT')->field($field)->where($map)->paginate(10);
        return $this->fetch('unorder', ['list' => $list]);  
	}
}
